//
//  BNDOfficeDocDetailsVc.m
//  OfficeDoc
//
//  Created by pie on 2022/4/3.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDOfficeDocDetailsVc.h"
#import "BNDOfficeDocDetailsTopView.h"
#import "BNDOfficeDocCTtachmentView.h"
#import "BNDOfficeDocReadView.h"
#import "BNDODAssociatedView.h"
#import "BNDOfficeDocSignedView.h"
#import "LVHUD.h"

@interface BNDOfficeDocDetailsVc ()
@property (strong, nonatomic) UIScrollView *detailsScrollView;
@property (strong, nonatomic) UIView *conatinerView;
@property (strong, nonatomic) BNDOfficeDocDetailsTopView *topView;
@property (strong, nonatomic) BNDODAssociatedView *associatedView;
@property (strong, nonatomic) BNDOfficeDocCTtachmentView *attachmentView;
@property (strong, nonatomic) BNDOfficeDocReadView *readView;
@property (strong, nonatomic) BNDOfficeDocSignedView *signedView;
@end

@implementation BNDOfficeDocDetailsVc

+(void)bndPushOfficeDocDetailsVc:(UIViewController *)topVc  officeDocId:(NSString * _Nullable )officeDocId
{
    BNDOfficeDocDetailsVc * sign = BNDOfficeDocDetailsVc.new;
    sign.officeDocId = officeDocId;
    [topVc.navigationController pushViewController:sign animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if(_attachmentView){
        [_associatedView reload];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"公告详情";
    if(self.model == nil&&self.officeDocId.length>0){
        self.model = [BNDOfficeDocModel new];
        self.model.modelID = self.officeDocId;
    }
    
    if(self.model.modelID.length ==0){
        [self handleCreateView];
    }else{
        @weakify(self)
        [LVHUD showLoding:self.view];
        [BNDApiManager  officeDocDetails:self.model.modelID success:^(BNDOfficeDocModel * _Nonnull model) {
            @strongify(self)
            [LVHUD dismissLoding:self.view];
            if(model == nil){
                [ReminderHUD showReminderText:@"公告已撤回" delayTime:3];
            }else{
                self.model = model;
                [self handleCreateView];
            }
        
        } failure:^(NSError * _Nullable error) {
            @strongify(self)
            [LVHUD dismissLoding:self.view];
        }];
    }
}

- (void)handleCreateView
{
    [self.view addSubview:self.detailsScrollView];
    [self.detailsScrollView addSubview:self.conatinerView];
    [self.conatinerView addSubview:self.topView];
    [self.conatinerView addSubview:self.associatedView];
    [self.conatinerView addSubview:self.attachmentView];
    [self.conatinerView addSubview:self.readView];
    [self.conatinerView addSubview:self.signedView];
    
    _topView.model = self.model;
    _attachmentView.model = self.model;
    _readView.model = self.model;
    _signedView.model = self.model;
    
    [self handdleData];
    [self layOutView];
}

- (void)handdleData
{
    // 请求公告公文内容
    @weakify(self)
    [BNDApiManager officeDocRequestArchiveController:self.model.docId success:^(BNDFileInfo * _Nonnull info) {
        @strongify(self)
        self.associatedView.model = self.model;
        self.associatedView.info = info;
    } failure:^(NSError * _Nullable error) {
        @strongify(self)
        self.associatedView.model = self.model;
        self.associatedView.info = [BNDFileInfo new];
    }];

    //
    //请求阅读统计
    if([BNDPreference sharedInstance].isAdmin){
        [BNDApiManager  officeDocStatistics:self.model.modelID success:^(NSString * _Nonnull readCount, NSString * _Nonnull readHasBeenSigned, NSString * _Nonnull readUnsigned, NSString * _Nonnull unreadCount) {
            @strongify(self)
            [self.readView handleData:readCount readHasBeenSigned:readHasBeenSigned readUnsigned:readUnsigned unreadCount:unreadCount];
        } failure:^(NSError * _Nullable error) {
            
        }];
    }
    
    //    修改公告已读状态
    if(self.model.recordId.length >0&&self.model.recordType==0){
        [BNDApiManager officeDocRequestRead:self.model.recordId success:^{
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationRefreshSuccess object:@"刷新列表"];
        } failure:^(NSError * _Nullable error) {
            
        }];
    }
}

- (void)layOutView
{
    [self.detailsScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(BNDNavigationBarHeight());
        make.left.right.bottom.equalTo(self.view);
        make.width.mas_equalTo(kDeviceWidth);
        make.height.mas_equalTo((kDeviceHeight -BNDNavigationBarHeight()));
    }];
    
    [self.conatinerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.detailsScrollView);
        make.width.equalTo(self.detailsScrollView);
    }];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.conatinerView.mas_top);
        make.left.right.equalTo(self.conatinerView);//左右约束
        //        make.bottom.equalTo(self.conatinerView);//这个不能忘记
    }];
    
    [self.associatedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(16);
        make.left.right.equalTo(self.conatinerView);//左右约束
    }];
    
    [self.attachmentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.associatedView.mas_bottom).offset(16);
        make.left.right.equalTo(self.conatinerView);//左右约束
    }];
    
    
    [self.readView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.attachmentView.mas_bottom).offset(0);
        make.left.right.equalTo(self.conatinerView);//左右约束
        make.height.mas_equalTo([BNDPreference sharedInstance].isAdmin?50:0);
    }];
    
    [self.signedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.readView.mas_bottom).offset(30);
        make.left.right.equalTo(self.conatinerView);//左右约束
        make.height.mas_equalTo(110);
        make.bottom.equalTo(self.conatinerView);//这个不能忘记
    }];
    
}

- (UIScrollView *)detailsScrollView
{
    if (!_detailsScrollView) {
        _detailsScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, kDeviceWidth, kDeviceHeight)];
        _detailsScrollView.backgroundColor = UIColor.whiteColor;
        [_detailsScrollView setScrollsToTop:NO];
        [_detailsScrollView setPagingEnabled:NO];
        [_detailsScrollView setShowsHorizontalScrollIndicator:NO];
        _detailsScrollView.bounces = NO;
        
    }
    return _detailsScrollView;
}

- (UIView*)conatinerView
{
    if(!_conatinerView){
        _conatinerView = [[UIView alloc] init];
    }
    return _conatinerView;
}

- (BNDOfficeDocDetailsTopView*)topView
{
    if(!_topView){
        _topView = [[BNDOfficeDocDetailsTopView alloc] init];
        _topView.backgroundColor = UIColor.whiteColor;
    }
    return _topView;
}

- (BNDODAssociatedView*)associatedView
{
    if(!_associatedView){
        _associatedView = [[BNDODAssociatedView alloc] init];
    }
    return _associatedView;
}


- (BNDOfficeDocCTtachmentView*)attachmentView
{
    if(!_attachmentView){
        _attachmentView = [[BNDOfficeDocCTtachmentView alloc] init];
    }
    return _attachmentView;
}

- (BNDOfficeDocReadView*)readView
{
    if(!_readView){
        _readView = [[BNDOfficeDocReadView alloc] init];
        self.readView.hidden = ![BNDPreference sharedInstance].isAdmin;
    }
    return _readView;
}

- (BNDOfficeDocSignedView*)signedView
{
    if(!_signedView){
        _signedView = [[BNDOfficeDocSignedView alloc] init];
    }
    return _signedView;
}

@end
